/**
 * 文件名：WidgetVersionApplicationImpl.java
 *
 * 版本信息：v1.0
 * 日期：2013-6-26
 * Copyright  Corporation 2013 
 * 版权所有
 *
 */

package com.foreveross.bsl.mam.model.application.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.query.Query;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.common.utils.web.QueryFilter;
import com.foreveross.bsl.common.utils.web.QueryFilter.Operator;
import com.foreveross.bsl.common.utils.web.QuerySpecs;
import com.foreveross.bsl.mam.app.application.AppVersionWidgetVersionApplication;
import com.foreveross.bsl.mam.model.app.domain.AppVersion;
import com.foreveross.bsl.mam.model.app.domain.SettingCategory;
import com.foreveross.bsl.mam.model.application.WidgetVersionApplication;
import com.foreveross.bsl.mam.model.application.vo.WidgetCategoryVO;
import com.foreveross.bsl.mam.model.application.vo.WidgetVO;
import com.foreveross.bsl.mam.model.application.vo.WidgetVersionVO;
import com.foreveross.bsl.mam.model.model.domain.Widget;
import com.foreveross.bsl.mam.model.model.domain.WidgetVersion;
import com.foreveross.bsl.mongodb.repository.DynamicQueryBuilder;

/**
 * 此类描述的是：模块版本实现接口类
 * 
 * @author: panhm@foreveross.com
 * @version: 2013-6-26 下午8:48:46
 */
@Named("widgetVersionApplication")
public class WidgetVersionApplicationImpl implements WidgetVersionApplication {

	@Inject
	private AppVersionWidgetVersionApplication appVersionWidgetVersionApplication;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.mam.model.application.WidgetVersionApplication#saveEntity
	 * (com.foreveross.bsl.mam.model.application.vo.WidgetVersionVO)
	 */

	@Override
	public WidgetVersionVO saveEntity(WidgetVersionVO entity) {
		// TODO Auto-generated method stub
		WidgetVersion version = new WidgetVersion();
		BeanHelper.copyProperties(version, entity);
		version.save();
		BeanHelper.copyProperties(entity, version);
		return entity;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.foreveross.bsl.mam.model.application.WidgetVersionApplication#
	 * updateEntity(java.lang.String,
	 * com.foreveross.bsl.mam.model.application.vo.WidgetVersionVO)
	 */

	@Override
	public WidgetVersionVO updateEntity(String id, WidgetVersionVO entity) {
		// TODO Auto-generated method stub
		// 更新版本信息
		WidgetVersion version = new WidgetVersion();
		version = (WidgetVersion) version.findOne(id);
		version.setVersion(entity.getVersion());
		version.setBuild(entity.getBuild());
		version.setBoundle(entity.getBoundle());
		version.setRelease_not(entity.getRelease_not());
		version.setModified_at(new Date());
		version.save();
		BeanHelper.copyProperties(entity, version);
		return entity;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.foreveross.bsl.mam.model.application.WidgetVersionApplication#
	 * deleteEntity(java.lang.String)
	 */

	@Override
	public void deleteEntity(String id) {
		// TODO Auto-generated method stub
		WidgetVersion version = new WidgetVersion();
		// 删除关联关系
		version = (WidgetVersion) version.findOne(id);
		appVersionWidgetVersionApplication.deleteWidget(version.getWidget_id(),
				version.getVersion());
		// 删除
		version.delete(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.mam.model.application.WidgetVersionApplication#getEntity
	 * (java.lang.String)
	 */

	@Override
	public WidgetVersionVO getEntity(String id) {
		// TODO Auto-generated method stub
		// 根据版本id查询版本信息
		WidgetVersionVO vo = new WidgetVersionVO();
		WidgetVersion wversion = new WidgetVersion();
		wversion = (WidgetVersion) wversion.findOne(id);
		BeanHelper.copyProperties(vo, wversion);

		// 根据模块id查询模块信息
		WidgetVO widgetVo = new WidgetVO();
		Widget wt = new Widget();
		wt = (Widget) wt.findOne(wversion.getWidget_id());
		if (wt != null) {
			BeanHelper.copyProperties(widgetVo, wt);
			// 获取模块分类
			SettingCategory widgetCategory = new SettingCategory();
			WidgetCategoryVO categoryVo = new WidgetCategoryVO();
			if (widgetVo.getCategory_id() != null) {
				widgetCategory = (SettingCategory) widgetCategory
						.findOne(widgetVo.getCategory_id());
				BeanHelper.copyProperties(categoryVo, widgetCategory);
				widgetVo.setCategoryVO(categoryVo);
				vo.setWidgetVo(widgetVo);
			}
		}

		return vo;
	}

	@SuppressWarnings("unchecked")
	@Override
	public WidgetVersionVO checkIdentity(String identify, String version) {
		// TODO Auto-generated method stub
		Widget widget = new Widget();
		List<Widget> widgetList = widget.findListByProperty("identify",
				OperatEnum.EQ, identify);
		if (widgetList != null && widgetList.size() > 0) {
			WidgetVersion wVersion = new WidgetVersion();
			widget = widgetList.get(0);
			WidgetVO widgetVo = new WidgetVO();
			BeanHelper.copyProperties(widgetVo, widget);
			List<WidgetVersion> wversionList = wVersion.findListByProperty(
					new String[] { "widget_id", "version" }, new OperatEnum[] {
							OperatEnum.EQ, OperatEnum.EQ }, new String[] {
							widget.getId(), version });
			WidgetVersionVO wversionVO;
			if (wversionList != null && wversionList.size() > 0) {
				wversionVO = new WidgetVersionVO();
				wVersion = wversionList.get(0);
				BeanHelper.copyProperties(wversionVO, wVersion);

				wversionVO.setWidgetVo(widgetVo);
				return wversionVO;
			}
		}
		return null;
	}

	@SuppressWarnings({ "unchecked" })
	@Override
	public List<WidgetVersionVO> findByWidgetId(String widgetId) {

		List<WidgetVersionVO> list = new ArrayList<WidgetVersionVO>();
		List<QueryFilter> filters = new ArrayList<QueryFilter>();
		QueryFilter widgetfilter = new QueryFilter("widget_id", Operator.EQ,
				widgetId);
		filters.add(widgetfilter);
		DynamicQueryBuilder byQuerySpecs = new DynamicQueryBuilder();
		QuerySpecs querySpecs = new QuerySpecs(filters);
		Map<String, Object> paramsOrder = new HashMap<String, Object>();
		paramsOrder.put("created_at", Direction.DESC);
		querySpecs.setOrdersByParamsMap(paramsOrder);
		Query query = byQuerySpecs.byQuerySpecs(querySpecs);
		WidgetVersion wVersion = new WidgetVersion();
		List<WidgetVersion> listVersion = wVersion.findListByQuery(query);
		if (listVersion != null) {
			WidgetVersionVO vo;
			for (int i = 0; i < listVersion.size(); i++) {
				vo = new WidgetVersionVO();
				BeanHelper.copyProperties(vo, listVersion.get(i));
				list.add(vo);
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public WidgetVersionVO widgetVersion(String widgetId, String version) {
		WidgetVersion widgetVersion = new WidgetVersion();
		List<WidgetVersion> wversionList = widgetVersion.findListByProperty(
				new String[] { "widget_id", "version" }, new OperatEnum[] {
						OperatEnum.EQ, OperatEnum.EQ }, new String[] {
						widgetId, version });
		WidgetVersionVO wversionVO;
		if (wversionList != null && wversionList.size() > 0) {
			wversionVO = new WidgetVersionVO();
			widgetVersion = wversionList.get(0);
			BeanHelper.copyProperties(wversionVO, widgetVersion);
			return wversionVO;
		}

		return null;
	}

}
